package com.zhao.fishingguideserver.mapper;

import com.zhao.fishingguideserver.POJO.PostLike;
import org.apache.ibatis.annotations.*;

@Mapper
public interface PostLikeMapper {

    @Insert("INSERT INTO post_likes(post_id, user_id) VALUES(#{postId}, #{userId})")
    int insert(PostLike postLike);

    @Delete("DELETE FROM post_likes WHERE post_id = #{postId} AND user_id = #{userId}")
    int deleteByPostIdAndUserId(@Param("postId") Integer postId, @Param("userId") Integer userId);

    @Delete("DELETE FROM post_likes WHERE post_id = #{postId}")
    int deleteByPostId(@Param("postId") Integer postId);

    @Delete("DELETE FROM post_likes WHERE user_id = #{userId}")
    int deleteByUserId(@Param("userId") Integer userId);

    /**
     * 检查用户是否已点赞帖子
     */
    @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId} AND user_id = #{userId}")
    int checkLike(@Param("postId") Integer postId, @Param("userId") Integer userId);

    /**
     * 统计帖子的点赞数
     */
    @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
    int countByPostId(@Param("postId") Integer postId);

    /**
     * 统计用户的点赞数
     */
    @Select("SELECT COUNT(*) FROM post_likes WHERE user_id = #{userId}")
    int countByUserId(@Param("userId") Integer userId);
}
